package com.icesoft.system.starter;

import com.icesoft.system.entity.Company;
import com.icesoft.system.entity.Org;
import com.icesoft.system.service.impl.CompanyServiceImpl;
import com.icesoft.system.service.impl.OrgServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Order(Integer.MIN_VALUE)
@Component
@RequiredArgsConstructor
public class CompanySettingStarter implements ApplicationRunner {

	private final CompanyServiceImpl companyService;

	private final OrgServiceImpl orgService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		if (companyService.count() == 0) {
			Company company = new Company();
			company.setId(1L);
			company.setName("系统公司");
			company.setEditable(false);
			company.setAppletAppid("wx545c09b196624fc2");
			company.setAppletSecret("8f8e24363aa5f4b9a3c766ed480b5843");
			companyService.createEntity(company);

			Org org01 = new Org();
			org01.setName("子部门1");
			org01.setCompanyId(company.getId());
			org01.setEditable(true);
			org01.setPid(1L);
			org01.setSort(1);
			org01.setStatus(true);
			orgService.createEntity(org01);

			Org org02 = new Org();
			org02.setName("子部门2");
			org02.setCompanyId(company.getId());
			org02.setEditable(true);
			org02.setPid(1L);
			org02.setSort(2);
			org02.setStatus(true);
			orgService.createEntity(org02);

			Org org11 = new Org();
			org11.setName("子子部门");
			org11.setCompanyId(company.getId());
			org11.setEditable(true);
			org11.setPid(org01.getId());
			org11.setSort(1);
			org11.setStatus(true);
			orgService.createEntity(org11);

			Org org21 = new Org();
			org21.setName("子子部门");
			org21.setCompanyId(company.getId());
			org21.setEditable(true);
			org21.setPid(org02.getId());
			org21.setSort(1);
			org21.setStatus(true);
			orgService.createEntity(org21);
		}
	}

}
